﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace App.Models
{
    public  class ReportClass
    {
        //每个死者的费用项目单打印
        public static List<vi_ServiceDetail> GetAllServiceDetail(string deadNo)
        {
            using (var dbf = new FuneralMisDBEntities())
            {                
                var x = from c in dbf.vi_ServiceDetail
                        where c.DeadNO == deadNo
                        select c;
                return x.ToList();
            }
        }
        //每个死者的所有费用统计
        public static List<vi_ServiceDetail> GetDeadFeeBill(string deadNo,string deadName)
        {
            using (var dbe = new FuneralMisDBEntities())
            {
                var list = dbe.vi_ServiceDetail.Where(m => m.DeadNO.Contains(deadNo) && m.DeadName.Contains(deadName));
                return list.ToList();
            }
        }

        //单位的收入月报表
        public static List<vi_ServiceDetail> GetServiceDetailByMonth(string dt, string type) {
            var dbf = new FuneralMisDBEntities();
            if(string.IsNullOrEmpty(dt)){
                var x = from c in dbf.vi_ServiceDetail orderby c.ItemNO,c.Price                    
                    select c;
                return x.ToList();
            }
            DateTime date = DateTime.Parse(dt);
            if (type == "1") {  //年报
                var x = from c in dbf.vi_ServiceDetail
                    orderby c.ItemNO, c.Price
                    where (c.PayTime != null) && c.PayTime.Value.Year==date.Year
                    select c;
                return x.ToList();
            }
            else if (type == "2")
            {//月报
                var x = from c in dbf.vi_ServiceDetail
                        orderby c.ItemNO, c.Price
                        where (c.PayTime != null) &&
                        c.PayTime.Value.Year == date.Year &&
                        c.PayTime.Value.Month == date.Month
                        select c;
                return x.ToList();
            }
            else{ //日报
                var x = from c in dbf.vi_ServiceDetail
                        orderby c.ItemNO, c.Price
                        where (c.PayTime != null) &&
                        c.PayTime.Value.Year == date.Year &&
                        c.PayTime.Value.Month == date.Month && 
                        c.PayTime.Value.Day == date.Day
                        select c;
                return x.ToList();
            }
        }
        //派车单
        public static List<tb_UseVehicleRecords> GetUseVehicleRecord(string useVehicleNo) {
            var dbf = new FuneralMisDBEntities();
            var x = from c in dbf.tb_UseVehicleRecords 
                    where c.UseVehicleNO == useVehicleNo 
                    select c;
            return x.ToList();
        }
        //车辆使用情况查询列表
        public static List<vi_UseVehicle> GetUseVehicleRecord(DateTime st,DateTime et,string driverNO,string vehicleNO)
        {
            var dbe = new FuneralMisDBEntities();
            var res = dbe.vi_UseVehicle.Where(m => (m.DepartureTime == null || (st <= m.DepartureTime) && et >= m.DepartureTime));
            if (!string.IsNullOrWhiteSpace(driverNO) && !string.IsNullOrWhiteSpace(vehicleNO))
            {
                res = res.Where(m => (m.Name.Contains(driverNO) && m.VehicleNO.Contains(vehicleNO))).OrderBy(m => m.Name);
            }
            else if (!string.IsNullOrWhiteSpace(driverNO))
            {
                res = res.Where(m => m.Name.Contains(driverNO)).OrderBy(m => m.Name);
            }
            else if (!string.IsNullOrWhiteSpace(vehicleNO))
            {
                res = res.Where(m => m.VehicleNO.Contains(vehicleNO)).OrderBy(m => m.VehicleNO);
            }
            return res.ToList();            
        }

        //按时间段统计单位的各项收入
        public static List<vi_ServiceDetail> GetFeesSummaryByTimeSpan(DateTime st, DateTime et) {
            var dbe = new FuneralMisDBEntities();
            var list = dbe.vi_ServiceDetail.Where(m => m.PayTime >= st && m.PayTime <= et);
            return list.ToList();
        }
    }
}